/**
 * 
 */
package org.statewalker.graph;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * A default sub-class of the {@link AbstractNodeWalker} class which implements
 * the stack of nodes using the standard {@link Stack} java class.
 * 
 * @author kotelnikov
 */
public class NodeWalker<T, E extends Throwable>
    extends
    AbstractNodeWalker<T, E> {

    private List<T> fStack;

    public NodeWalker(INodeWalkerSource<T, E> source, T topNode) {
        super(source, topNode);
    }

    @Override
    protected void clearNodeStack() {
        List<T> stack = getStack();
        stack.clear();
    }

    /**
     * @see org.statewalker.graph.AbstractNodeWalker#getPeekNode()
     */
    @Override
    public T getPeekNode() {
        List<T> stack = getStack();
        return !stack.isEmpty() ? stack.get(stack.size() - 1) : null;
    }

    public List<T> getStack() {
        if (fStack == null)
            fStack = new ArrayList<T>();
        return fStack;
    }

    /**
     * @see org.statewalker.graph.AbstractNodeWalker#popNode()
     */
    @Override
    protected T popNode() {
        List<T> stack = getStack();
        return !stack.isEmpty() ? stack.remove(stack.size() - 1) : null;
    }

    /**
     * @see org.statewalker.graph.AbstractNodeWalker#pushNode(java.lang.Object)
     */
    @Override
    protected void pushNode(T currentNode) {
        List<T> stack = getStack();
        stack.add(currentNode);
    }

}
